<template>
	<div>
		<van-nav-bar
			title="地址信息"
			left-text="返回"
			left-arrow
			@click-left="ret" />
		<!-- 修改默认 -->
		<template v-if="!isSelect">
			<van-address-list
				v-model="chosenAddressId"
				:list="list"
				default-tag-text="默认"
				@select="onDefault"
				@add="onAdd"
				@edit="onEdit" />
		</template>
		<!-- 选择收获地址 -->
		<template v-else>
			<van-address-list
				v-model="chosenAddressId"
				:list="list"
				default-tag-text="默认"
				@select="onSelect"
				@add="onAdd"
				@edit="onEdit" />
		</template>

		<van-overlay :show="isOverlay"></van-overlay>

		<!-- 网络请求加载状态 -->
		<van-overlay :show="isLoading" style="display: flex; justify-content: center; align-items: center;">
			<van-loading vertical>
				<template #icon>
					<van-icon name="star-o" size="30" />
				</template>
				加载中...
			</van-loading>
		</van-overlay>
	</div>
</template>

<script>
	import { showFailToast, showSuccessToast, showToast } from "vant";
	export default {
		created() {
			this.isSelect = this.$route.query.buyId||this.$route.query.shopId? true: false;
			if (this.isSelect) {
				if (this.$route.query.buyId) {
					this.id = this.$route.query.buyId;
				}else if (this.$route.query.shopId) {
					this.id = this.$route.query.shopId;
				}
			}
			this.createdData(this.id);
		},
		data() {
			return {
				chosenAddressId: true,
				list: [],
				isOverlay: false,
				isSelect: false,
				id: null,
				select: null,
				isLoading: false,
			};
		},
		methods: {
			ret() {
				this.$router.go(-1);
			},
			// 初始化
			async createdData() {
				this.isLoading = true;
				let res = await this.$api.UserAddressInfo({ busid: this.$cookies.get("User").id });
				if (res.code == 1) {
					res.data.forEach((item) => {
						this.list.push({
							id: item.id,
							name: item.consignee,
							tel: item.mobile,
							address: item.region,
							isDefault: item.status == 0 ? false : true,
						});
					});
				} else {
					showToast({ message: res.msg });
				}
				this.isLoading = false;
			},
			// 添加
			onAdd() {
				this.$router.push("/address/add");
			},
			// 跳转编辑
			onEdit(val) {
				this.$router.push({ path: "/address/edit", query: { addressId: val.id } });
			},
			// 切换默认地址
			async onDefault(val) {
				this.isLoading = true;
				let res = await this.$api.AddressAdd({
					DefaultId: val.id,
					busid: this.$cookies.get("User").id,
				});
				if (res.code == 1) {
					let _this = this;
					this.isOverlay = true;
					showSuccessToast({
						message: res.msg,
						duration: 1000,
						onClose() {
							_this.$router.go(0);
						},
					});
				} else {
					showFailToast({
						message: res.msg,
						duration: 1000,
					});
				}
				this.isLoading = false;
			},
			// 选择收货地址
			async onSelect(val) {
				// 判断是购物车购买还是商品购买
				if (this.$route.query.buyId) {
					this.$router.push({ path: "/shopcart/buy", query: { address: [val.id, val.name, val.tel].toString(), buyId: this.id } });
				}else if (this.$route.query.shopId) {
					this.$router.push({ path: "/shopcart/buy", query: { address: [val.id, val.name, val.tel].toString(), shopId: this.id } });
				}
			}
		},
	};
</script>

<style></style>
